<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true">
      <el-form-item>
        <el-input
          v-model="queryParams.searchValue"
          placeholder="请输入姓名"
          clearable
          style="width: 240px"
        />
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="queryDate"
          type="daterange"
          value-format="yyyy-MM-dd"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQueryFn">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQueryFn">重置</el-button>
        <el-button type="primary" icon="el-icon-download" @click="exportQueryFn">导出</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="reportData">
      <el-table-column align="center" label="序号" width="50">
        <template slot-scope="scope">
          {{scope.$index+1}}
        </template>
      </el-table-column>
      <el-table-column label="姓名" prop="userName" />
      <el-table-column label="项目" prop="projectName" />
      <el-table-column label="工时类型" prop="workTypeName" />
      <el-table-column label="工时数" prop="workHourTotal" />
    </el-table>
  </div>
</template>

<script>
  import { apiUserStatList, apiExportUserStatList } from "@/api/pmp/api.report";

  export default {
    name: 'workhourPersonalReport',
    data () {
      return {
        // 遮罩层
        loading: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          roleName: undefined,
          roleKey: undefined,
          status: undefined
        },
        total: 0,
        // 报表数据
        reportData: [],
        // 时间框查询数据集
        queryDate: []

      };
    },
    created() {

    },
    methods: {
      exportQueryFn() {
        let query = {...this.queryParams};
        if(this.queryDate || this.queryDate.length > 2){
          query.startTime = this.queryDate[0];
          query.endTime = this.queryDate[1];
        }
        apiExportUserStatList(query).then(
          response => {
            console.log('导出结果', response);
            this.download(response.msg);
          }
        );
      },
      /** 查询角色列表 */
      getListFn() {
        let query = {...this.queryParams};
        this.loading = true;
        if(this.queryDate || this.queryDate.length > 2){
          query.startTime = this.queryDate[0];
          query.endTime = this.queryDate[1];
        }
        apiUserStatList(query).then(
          response => {
            this.reportData = response.rows;
            this.total = response.total;
            this.loading = false;
          }
        );
      },
      /** 搜索按钮操作 */
      handleQueryFn() {
        this.getListFn();
      },
      /** 重置按钮操作 */
      resetQueryFn() {
        this.queryDate = [];
        this.resetForm("queryForm");
      },
    }
  };
</script>

